﻿using Cemit.Awaiter;
using Cemit.PolyProto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cemit.NetFramework
{
    class LobbyClient : PolyClient
    {
        private const string CONFIRM_MATCH_TAG = "confirm match";

        public async Task<LoginResult> Login(string name)
        {
            return await socket.Login(name);
        }

        /// <summary>加入匹配</summary>
        public void JoinMatch(Action<MatchInfo> onMatchComplete, short peopleNumber)
        {
            socket.Send(new Msg_Lobby_JoinMatch() { FullNumber = peopleNumber })
                .SetConfirmCallback(CONFIRM_MATCH_TAG, () => onMatchComplete(new MatchInfo()));
        }

        /// <summary>可等待的方法，确认匹配，取得匹配结果</summary>
        public MonoAsyncAction<(MatchResult matchResult, GameRoomInfo roomInfo)> ConfirmMatch(MatchInfo matchInfo)
        {
            var action = new MonoAsyncAction<(MatchResult matchResult, GameRoomInfo roomInfo)>();

            socket.SendConfirm(CONFIRM_MATCH_TAG)
                .AddCallback<Msg_Lobby_MatchResult>(
                    msg => action.ReportResult((msg.MathResult, msg.GameRoomInfo)));

            return action;
        }
    }
}
